﻿using MailModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MailDAL
{
    public class UnitOfWork : IDisposable
    {
        private MailToolEntities entities;
        public MailToolEntities Entities
        {
            get
            {
                if (entities == null)
                    entities = new MailToolEntities();
                return entities;
            }
        }

        public bool ProxyCreationEnabled
        {
            get { return Entities.Configuration.ProxyCreationEnabled; }
            set { Entities.Configuration.ProxyCreationEnabled = value; }
        }

        public bool ValidateOnSaveEnabled
        {
            get { return Entities.Configuration.ValidateOnSaveEnabled; }
            set { Entities.Configuration.ValidateOnSaveEnabled = value; }
        }

        private GenericRepository<User> userRepository;
        public GenericRepository<User> UserRepository
        {
            get
            {

                if (userRepository == null)
                    userRepository = new GenericRepository<User>(Entities);
                return userRepository;
            }
        }

        private GenericRepository<Template> templateRepository;
        public GenericRepository<Template> TemplateRepository
        {
            get
            {

                if (templateRepository == null)
                    templateRepository = new GenericRepository<Template>(Entities);
                return templateRepository;
            }
        }

        private GenericRepository<MailLogs> mailLogsRepository;
        public GenericRepository<MailLogs> MailLogsRepository
        {
            get
            {

                if (mailLogsRepository == null)
                    mailLogsRepository = new GenericRepository<MailLogs>(Entities);
                return mailLogsRepository;
            }
        }

        private GenericRepository<AddressList> addressListRepository;
        public GenericRepository<AddressList> AddressListRepository
        {
            get
            {

                if (addressListRepository == null)
                    addressListRepository = new GenericRepository<AddressList>(Entities);
                return addressListRepository;
            }
        }

        private GenericRepository<MailFromList> mailFromListRepository;
        public GenericRepository<MailFromList> MailFromListRepository
        {
            get
            {

                if (mailFromListRepository == null)
                    mailFromListRepository = new GenericRepository<MailFromList>(Entities);
                return mailFromListRepository;
            }
        }

        public void Save()
        {
            Entities.SaveChanges();
        }

        public UnitOfWork()
        {
            if (disposed)
                throw new ObjectDisposedException(GetType().Name);
        }

        ~UnitOfWork()
        {
            Dispose(false);
        }

        private bool disposed = false;
        protected virtual void Dispose(bool disposing)
        {
            if (!disposed)
            {
                if (disposing)
                {
                    if (entities != null)
                        entities.Dispose();
                }
            }
            disposed = true;
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
    }
}
